﻿/*******************************************************************************
* Copyright (C) 2006-2011 newportcn
* @author Jiyuan
*******************************************************************************/

KindEditor.plugin('calking', function (K) {
    var self = this, name = 'calking', lang = self.lang(name + '.');
    var kedit = K;
    var blankKey = 0;
    self.clickToolbar(name, function () {
        blankKey++;
        if (self.cmd.sel.rangeCount == 0) { alert("请选中插入填空框的位置"); return; }
        self.insertHtml(_buildBlankTextBox(blankKey));
        ShowHtml(blankKey, self.html());
        self.appendHtml("");
        //        var range = self.cmd.range;
        //var range = window.frames[0].getSelection().getRangeAt(0);
        //if (window._blankKey == undefined) {
        //    window._blankKey = 1;
        //}
        //if (insertBlankTextBox(self, range, _buildBlankTextBox(window._blankKey))) {

        //    //ShowOption(self);
        //    //alert(blankTextBoxKey);
        //    ShowHtml(window._blankKey - 1);
        //    //self.appendHtml("");
        //}
    });
});

//思路：
//----触发点:点击控件时，无需判断，只需要执行添加下划线操作，以及生成数字,调用外联函数，判断现有下划线的个数
//如果下划线个数与答案项数不等，将文本放入一个div中，获取下划线所有编号，删除不需要的答案项



//生成下划线
function CreateBlankTextBox(key) {
    return '<span  class="blankTB"  title="' + key + '" > __' + key + '__</span>';
}




function insertBlankTextBox(editor, range, blankTextBox) {
    if (range.startContainer != range.endContainer) {
        alert("请选择连续的词语！");
        return;
    }
    //var element = range.commonAncestorContainer;
    var titleContent = range.startContainer.data;
    if (titleContent != undefined) {
        $(range.startContainer).replaceWith(titleContent.substring(0, range.startOffset) + blankTextBox + titleContent.substring(range.endOffset, titleContent.length));
        //        self.focus();
    }
    else {
        $(range.startContainer).html(blankTextBox);
    }
    _manageKey();
    return true;
}

function _buildBlankTextBox(key) {
    return '<span  class="blankTB_' + key + '" title="' + key + '">__' + key + '__</span>';
}

function _manageKey() {
    if (window._blankKey == undefined) {
        window._blankKey = 1;
     }
    window._blankKey += 1;
}

(function ($) {
    $.removeBlankTextBoxByKey = function (key) {
        try {
            var filter = "span[class='blankTB_" + key + "']";
            window._blankKey -= 1;
            $(filter, window.frames[0].document).each(function () {
                _reSort(key);
                $(this).replaceWith("");
            });
        }
        catch (e) {
            alert(e);
        }
    }
})(jQuery);

function _reSort(key) {
    var filter = "span[class^='blankTB_']";
    $(filter).each(function () {
        var oldValue = $(this).attr("class");
        oldValue = oldValue.substring(oldValue.lastIndexOf("_"), oldValue.length);
        if (oldValue > key) {
            key -= 1;
            $(this).attr("class", "blankTB_'" + key + "'");
        }
    });
}